package com.example.videoproject.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.videoproject.pojo.Person;
import com.example.videoproject.pojo.Remark;
import com.example.videoproject.service.IPersonService;
import com.example.videoproject.service.IRemarkService;
import com.example.videoproject.utils.Code;
import com.example.videoproject.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 二组
 * @since 2022-07-23
 */
@RestController
@RequestMapping("/remark")
public class RemarkController {

    @Autowired
    private IRemarkService remarkService;


    @Autowired
    private IPersonService personService;

    //查询一级评论
    @PostMapping("getComment0")
    public Result getComment0(Integer vid){
        QueryWrapper<Remark> wrapper1=new QueryWrapper<>();
        wrapper1.eq("vid",vid);
        wrapper1.eq("parentrid",0);

        List<Remark> remarks = remarkService.list(wrapper1);

        return new Result(Code.SELECT_OK,remarks);
    }
    //查询二级评论
    @PostMapping("getComment1")
    public Result getComment1(Integer vid){
        //查询二级评论并分组
        QueryWrapper<Remark> wrapper=new QueryWrapper<>();
        wrapper.eq("vid",vid);
        wrapper.ne("parentrid",0);
        List<Remark> list = remarkService.list(wrapper);
        Map<Integer, List<Remark>> collect = list.stream()
                .sorted(Comparator.comparingInt(Remark::getRemarklike).reversed())
                .collect(Collectors.groupingBy(Remark::getParentrid));

        return new Result(Code.SELECT_OK,collect);
    }

    //评论
    @PostMapping("pushRemark")
    public Result pushRemark(Integer vid,Integer uid,Integer rid,String myReply){
        String strDateFormat = "yyyy/MM/dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        Person person = personService.getById(uid);
        Remark remark=new Remark(vid,uid,person.getUname(),rid,null,sdf.format(new Date()),null,myReply.trim(),person.getUimage(),null);
        boolean save = remarkService.save(remark);
        return new Result(Code.INSERT_OK,null);
    }

    //删除评论
    @PostMapping("deleteRemark")
    public Result deleteRemark(Integer rid){
        remarkService.removeById(rid);
        return new Result(Code.DELETE_OK,null);
    }

    @PostMapping("remarkLike")
    public Result remarkLike(Integer rid,boolean clickLike){
        QueryWrapper<Remark> wrapper=new QueryWrapper<>();
        wrapper.eq("rid",rid);
        Remark one = remarkService.getOne(wrapper);
        Integer remarklike = one.getRemarklike();
        if (clickLike){
            remarklike++;   //点赞就加1
        }else if(remarklike>0){
            --remarklike;   //取消点赞
        }
        one.setRemarklike(remarklike);
        boolean b = remarkService.updateById(one);
        int code = b?Code.UPDATE_OK:Code.UPDATE_ERR;

        return new Result(code,one);
    }

}

